using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Pokemon
{
    class Entry: IAnimation
    {
        #region Attribute

        private string _text;

        private Vector2 _position;

        private readonly Menu _owner;

#endregion

        #region Properties

        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; }
        }

        public Menu Owner
        {
            get { return _owner; }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="text"></param>
        public Entry(Menu menu, string text)
        {
            _owner = menu;
            _text = text;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="menu"></param>
        /// <param name="text"></param>
        /// <param name="position"></param>
        public Entry(Menu menu, string text, Vector2 position)
        {
            _owner = menu;
            _text = text;
            _position = position;
        }

#endregion

        #region Support Method

        /// <summary>
        /// Get distance LineSpacing
        /// </summary>
        /// <param name="gameScreen"></param>
        /// <returns></returns>
        public int GetHeight(GameScreen gameScreen)
        {
            return gameScreen.ScreenManager.SpriteFont.LineSpacing;
        }
        /// <summary>
        /// Get length Entry
        /// </summary>
        /// <param name="gameScreen"></param>
        /// <returns></returns>
        public int GetWidth(GameScreen gameScreen)
        {
            return (int)gameScreen.ScreenManager.SpriteFont.MeasureString(_text).X;
        }

#endregion

        #region Event: Raise when entry selected

        /// <summary>
        /// Event Raise when entry selected
        /// </summary>
        public event EventHandler Selected;

        /// <summary>
        /// Initialize for raising the Selected event.
        /// </summary>
        protected internal virtual void OnSelected()
        {
            if (Selected != null)
                Selected(this, null);
        }

        public void Draw(SpriteBatch spriteBatch, SpriteFont spriteFont)
        {                        
            Animation(spriteBatch);
        }

        public void Draw(SpriteBatch sp, SpriteFont spriteFont, Vector2 position, Color color)
        {
            Vector2 origin = new Vector2(0, (int)(spriteFont.LineSpacing / 2));

            sp.DrawString(spriteFont, _text, position, color, 0, origin, 1.0f, SpriteEffects.None, 0);            
        }
#endregion

        private bool _isStartup = false;
        private TimeSpan _animationTime = TimeSpan.Zero;
        public bool IsStartup
        {
            get { return _isStartup; }
            set { _isStartup = value; }
        }

        public TimeSpan AnimationTime
        {
            get { return _animationTime; }
            set { _animationTime = value; }
        }

        internal delegate void DoAnimation(object sender, TimeSpan animationTime);

        public event DoAnimation Animated;
       
        /// <summary>
        /// Khong biet phai ve the nao, uy thac cho Menu ve ho
        /// </summary>
        /// <param name="sp"></param>
        public void Animation(SpriteBatch sp)
        {            
            DoAnimation handler = Animated;
            if (handler != null) 
                handler(this, _animationTime);
        }

        public void Startup()
        {
            _isStartup = true;
            _animationTime = TimeSpan.Zero;
        }
    }
}
